#include "stm32f10x.h"                  // Device header
#include <stdio.h>
#include <stdarg.h>
#include "Motor.h"
#include "Bluetooth.h"

/*定义接收到的数据和标志位*/
uint8_t Bluetooth_RxData;
static Bluetooth_Flag Flag;

/**
  * @brief  蓝牙初始化
  * @param  无
  * @retval 无
  */
void Bluetooth_Init()
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);

	USART_InitTypeDef USART_InitStructure;
	USART_InitStructure.USART_BaudRate=9600;
	USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
	USART_InitStructure.USART_Parity=USART_Parity_No;
	USART_InitStructure.USART_StopBits=USART_StopBits_1;
	USART_InitStructure.USART_WordLength=USART_WordLength_8b;
	USART_Init(USART1,&USART_InitStructure);
	
	USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority=3;
	NVIC_Init(&NVIC_InitStructure);
	
	USART_Cmd(USART1,ENABLE);
}

/**
  * @brief  蓝牙发送一个字节数据
  * @param  Byte：要发送的数据
  * @retval 无
  */
void Bluetooth_SendByte(uint8_t Byte)
{
	USART_SendData(USART1,Byte);
	while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
}

/**
  * @brief  蓝牙发送一个数组
  * @param  Array：要发送的数组
  * @param  Length：数组的长度
  * @retval 无
  */
void Bluetooth_SendArray(uint8_t* Array,uint16_t Length)
{
	uint8_t i=0;
	for(i=0;i<Length;i++)
	{
		Bluetooth_SendByte(Array[i]);
	}
}

/**
  * @brief  蓝牙发送一个字符串
  * @param  String：要发送的字符串
  * @retval 无
  */
void Bluetooth_SendString(char* String)
{
	uint8_t i=0;
	for(i=0;String[i]!='\0';i++)
	{
		Bluetooth_SendByte(String[i]);
	}
}

/**
  * @brief  计算x的y次方
  * @param  x：底数
  * @param  y：次方数
  * @retval 计算出的结果
  */
uint32_t Pow(uint32_t x,uint8_t y)
{
	uint32_t Result;
	while(y--)
	{
		Result*=x;
	}
	return Result;
}

/**
  * @brief  蓝牙发送一个数字
  * @param  Number：要发送的数字
  * @param  Length：发送数字的长度
  * @retval 无
  */
void Bluetooth_SendNum(uint32_t Number,uint8_t Length)
{
	uint8_t i=0;
	for(i=0;i<Length;i++)
	{
		Bluetooth_SendByte((Number/Pow(10,Length-i-1)%10)+'0');
	}
}

/*采用江科大的源码，具体内容可以上网查*/
int fputc(int ch,FILE*f)//printf的原型
{
	Bluetooth_SendByte(ch);
	return ch;
}
//@brief 封装sprintf.
//@parm 接受格式化字符串.
//@parm 接受可变参数列表.
void Bluetooth_Printf(char* format,...)
{
	char String[100];
	va_list arg;//定义一个参数列表变量,va_list是一个类型名，arg是变量名.
	va_start(arg,format);//从format位置开始接受参数表，放在arg里面.
	//sprintf只能接受直接写的参数，对于封装格式要用vsprintf.
	vsprintf(String,format,arg);//打印位置是Sting,格式化字符串是format,参数表是arg.
	va_end(arg);//释放参数表..
	Bluetooth_SendString(String);
}

/**
  * @brief  蓝牙获取标志位
  * @param  Flag：要获取的标志位
  *	 参数可以是这些:
  *   @arg  Bluetooth_RxFlag        接收标志位
  *   @arg  Bluetooth_CtrCarFlag    控制小车标志位
  * @retval 获取到的标志位
  */
uint8_t Bluetooth_GetFlag(uint8_t BL_Flag)
{
	if(BL_Flag==BLUE_RX_FLAG)
	{
		return Flag.Bluetooth_RxFlag;
	}
	else if(BL_Flag==BLUE_CTRCAR_FLAG)
	{
		return Flag.Bluetooth_CtrCarFlag;
	}
	return 0;
}

/**
  * @brief  蓝牙获取数据
  * @param  无
  * @retval 获取到的数据
  */
uint8_t Bluetooth_GetRxData()
{
	return Bluetooth_RxData;
}

/**
  * @brief  蓝牙控制小车的运行状态
  * @param  无
  * @retval 无
  */
void Bluetooth_CtrMasterCar(SetMotor_Speed* BL_Speed)
{
	if(Flag.Bluetooth_RxFlag==1)  //不要使用Bluetooth_GetFlag与主函数冲突
	{
		if(Bluetooth_RxData==0x49)  //停止
		{
			/*这里可以在优化下，如进入/退出低功耗模式*/
			Flag.Bluetooth_CtrCarFlag=!Flag.Bluetooth_CtrCarFlag;
		}
		else if(Bluetooth_RxData==0x47&&Flag.Bluetooth_CtrCarFlag==1)  //向前
		{
			BL_Speed->Speed1=80;
			BL_Speed->Speed2=80;
			BL_Speed->Speed3=80;
			BL_Speed->Speed4=80;
		}
		else if(Bluetooth_RxData==0x4B&&Flag.Bluetooth_CtrCarFlag==1)  //向后
		{
			BL_Speed->Speed1=-80;
			BL_Speed->Speed2=-80;
			BL_Speed->Speed3=-80;
			BL_Speed->Speed4=-80;
		}
		else if(Bluetooth_RxData==0x48&&Flag.Bluetooth_CtrCarFlag==1)  //向左
		{
			BL_Speed->Speed1=0;
			BL_Speed->Speed2=100;
			BL_Speed->Speed3=0;
			BL_Speed->Speed4=100;
		}
		else if(Bluetooth_RxData==0x4A&&Flag.Bluetooth_CtrCarFlag==1)  //向右
		{
			BL_Speed->Speed1=100;
			BL_Speed->Speed2=0;
			BL_Speed->Speed3=100;
			BL_Speed->Speed4=0;
		}
		else if(Bluetooth_RxData==0x41&&Flag.Bluetooth_CtrCarFlag==1)  //右自旋
		{
			BL_Speed->Speed1=100;
			BL_Speed->Speed2=-100;
			BL_Speed->Speed3=100;
			BL_Speed->Speed4=-100;
		}
		else if(Bluetooth_RxData==0x42&&Flag.Bluetooth_CtrCarFlag==1)  //停止
		{
			BL_Speed->Speed1=0;
			BL_Speed->Speed2=0;
			BL_Speed->Speed3=0;
			BL_Speed->Speed4=0;
		}
		else if(Bluetooth_RxData==0x43&&Flag.Bluetooth_CtrCarFlag==1)  //加速
		{
			BL_Speed->Speed1+=30;
			BL_Speed->Speed2+=30;
			BL_Speed->Speed3+=30;
			BL_Speed->Speed4+=30;
		}
		else if(Bluetooth_RxData==0x44&&Flag.Bluetooth_CtrCarFlag==1)  //加速，下同
		{
			BL_Speed->Speed1-=10;
			BL_Speed->Speed2-=10;
			BL_Speed->Speed3-=10;
			BL_Speed->Speed4-=10;
		}
		else if(Bluetooth_RxData==0x45&&Flag.Bluetooth_CtrCarFlag==1)
		{
			BL_Speed->Speed1-=20;
			BL_Speed->Speed2-=20;
			BL_Speed->Speed3-=20;
			BL_Speed->Speed4-=20;
		}
		else if(Bluetooth_RxData==0x46&&Flag.Bluetooth_CtrCarFlag==1)
		{
			BL_Speed->Speed1-=30;
			BL_Speed->Speed2-=30;
			BL_Speed->Speed3-=30;
			BL_Speed->Speed4-=30;
		}
		
		/*******这里可以继续添加更多的命令...
		
								...******/
		if(Flag.Bluetooth_CtrCarFlag)
		{
			Motor_SetAllSpeed(BL_Speed);
			Bluetooth_RxData=0;  	//将接收到的数据清零，避免产生干扰
		}
		
		else if(Flag.Bluetooth_CtrCarFlag==0)
		{
			Flag.Bluetooth_RxFlag=0;
		}
	}
}

void USART1_IRQHandler()
{
	if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)
	{
		Bluetooth_RxData=USART_ReceiveData(USART1);  //获取接收到的数据并置标志位1
		Flag.Bluetooth_RxFlag=1;
		USART_ClearITPendingBit(USART1,USART_IT_RXNE);
	}
}

